package com.atguigu.controller;

import com.atguigu.controller.base.ModelBaseServlet;
import com.atguigu.pojo.BookstoreConstant;
import com.atguigu.pojo.ResultVO;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.JSONUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author luoyin
 * @Date 15:57 2022/7/18
 * @Description 用户操作控制层
 **/
@WebServlet("/user")
public class UserController extends ModelBaseServlet {

    //跳转到login页面
    public void toLoginPage(HttpServletRequest request, HttpServletResponse response) {

        try {
            processTemplate("pages/user/login", request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //跳转到登录成功页面
    public void toLoginSuccessPage(HttpServletRequest request, HttpServletResponse response) {

        try {
            processTemplate("pages/user/login_success", request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //跳转到regist页面
    public void toRegistPage(HttpServletRequest request, HttpServletResponse response) {

        try {
            processTemplate("pages/user/regist", request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //跳转到注册成功页面
    public void toRegistSuccessPage(HttpServletRequest request, HttpServletResponse response) {

        try {
            processTemplate("pages/user/regist_success", request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void login(HttpServletRequest request , HttpServletResponse response) throws Exception {
        //获取请求参数和值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调用service中的方法判断是否可以登录成功
        UserService userService = new UserServiceImpl();
        User dbUser = userService.loginService(username, password);
        ResultVO<Object> resultVO = null;
        if (dbUser == null) {
            //登录失败,显示错误信息
             resultVO = new ResultVO<>(false, "账号或密码错误", null);
        } else {
            //登录成功,
            request.getSession().setAttribute(BookstoreConstant.SESSION_KEY_USER,dbUser);
            resultVO = new ResultVO<>(true, "登录成功", dbUser);
        }
        JSONUtils.object2JsonStrAndresponse(response,resultVO);
    }

    //注册
    public void regist(HttpServletRequest request,HttpServletResponse response){
        //获取请求参数
        try {
            User user = new User();
            BeanUtils.populate(user,request.getParameterMap());

            //验证验证码
            String inputCode = request.getParameter("inputCode");
            String kaptchaCode = (String) request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
            if(!inputCode.equals(kaptchaCode)){
                request.setAttribute("existUser",user);
                request.setAttribute("kaptchaCode","验证码错误");
                toRegistPage(request,response);
                return;
            }
            //判断账户是否存在
            UserService userService = new UserServiceImpl();
            User dbuser = userService.selectByNameService(user.getUserName());
            if(dbuser != null){
                //账户存在,返回一个消息"账户已经存在"
                request.setAttribute("errorMsg","账号已经存在");
                toRegistPage(request,response);
                return;
            }
            //账户不存在,注册
            if(userService.registService(user) > 0){
                //注册成功,重定向到  pages/user/regist_success.html
                request.getSession().setAttribute("existUser",user);
                response.sendRedirect(request.getContextPath()+"/user?method=toRegistSuccessPage");
            }else {
                //注册失败,请求转发到  pages/user/regist.html
                toRegistPage(request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toRegistPage(request,response);
        }
    }

    //注销
    public void logout(HttpServletRequest request , HttpServletResponse response){
        Object attribute = request.getSession().getAttribute(BookstoreConstant.SESSION_KEY_USER);

        ResultVO<Object> resultVO = null;
        if( attribute == null){
            //注销失败,不在登录状态
            resultVO = new ResultVO<>(false,"注销失败",null);
        }else {
            //注销成功
            request.getSession().removeAttribute("existUser");
            resultVO = new ResultVO<>(true,"成功注销",null);
        }
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
